import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import java.io.File
import java.io.FileWriter
import java.io.IOException


@Composable
@Preview
fun App() {
    var fileNames by remember { mutableStateOf<List<String>>(mutableListOf()) }
    var filePath by mutableStateOf("")
    var showDialog by remember { mutableStateOf(false) }
    var showPath by remember { mutableStateOf(false) }

    @Throws(IOException::class)
    fun createFile(fileName: String, content: String) {
        val file = File(fileName)
        FileWriter(file).use { writer ->
            writer.write(content)
        }
    }


    @Composable
    fun showFileNameDialog() {
        var fileName by remember { mutableStateOf("") }
        var content by remember { mutableStateOf("") }
        var isDialogVisible by remember { mutableStateOf(true) }
        if (isDialogVisible) {
            AlertDialog(
                onDismissRequest = { isDialogVisible = false },
                title = { Text("输入文件名和内容") },
                text = {
                    Column {
                        TextField(value = fileName, onValueChange = { fileName = it })
                        TextField(value = content, onValueChange = { content = it })
                    }
                },
                confirmButton = {
                    Button(
                        onClick = {
                            try {
                                createFile(fileName, content)
                                fileNames = fileNames + fileName
                            } catch (e: IOException) {
                                e.printStackTrace()
                            }
                            isDialogVisible = false
                        }
                    ) {
                        Text("保存")
                    }
                },
                dismissButton = {
                    Button(onClick = { isDialogVisible = false }) {
                        Text("取消")
                    }
                }
            )
        }
    }


    @Composable
    fun FileItem(fileName: String, onClick: () -> Unit) {
        Button(onClick = onClick) {
            Text(fileName)
        }
        Button(long = {  }) {
            Text("编辑")
        }
        Button(onClick = { } ) {
            Text("删除")
        }

    }

    @Composable
    fun showFilePathDialog(fileName: String) {
        AlertDialog(
            onDismissRequest = {},
            title = { Text("文件路径") },
            text = {
                Text("当前文件路径：$fileName")
            },
            confirmButton = {
                Button(onClick = {
                    showPath = false
                }) {
                    Text("确定")
                }
            }
        )
    }

    @Composable
    fun FileManagerApp() {
        // 使用 mutableStateListOf 创建可变状态列表
        Column(
//        modifier = Modifier.fillMaxSize()
        ) {
            Button(
                onClick = {
                    showDialog = true
                }
            ) {
                Text("添加")
            }
            if (showDialog) {
                showFileNameDialog()
            }
            if(showPath){
                showFilePathDialog(filePath)
            }
            LazyVerticalGrid(
                columns = GridCells.Fixed(3),
                contentPadding = PaddingValues(8.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp),
                horizontalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                items(fileNames) { fileName ->
                    FileItem(fileName = fileName) {
                        filePath = fileName
                        showPath = true
                    }
                }
            }
        }
    }



    MaterialTheme {
        FileManagerApp()
    }


}

fun main() = application {
    Window(onCloseRequest = ::exitApplication) {
        App()
    }
}

